import JSZip from "jszip";
import moment from "moment";

export default {
    downloadZips(arr, companyName) {
        debugger
        const zip = new JSZip();
        arr.forEach(ele => {
            setTimeout(() => {
                if (ele.filePath === '') {
                    const folder = zip.folder(ele.companyName + '_' + ele.name + '_' + ele.companyId)
                    folder.file('', '')
                } else {
                    const folder = zip.folder(ele.companyName + '_' + ele.name + '_' + ele.companyId)
                    // 使用fetch获取图片的Blob数据
                    Promise.all([
                        fetch(ele.filePath).then(response => response.blob()),
                    ]).then(images => {
                        // 添加图片到zip文件中的文件夹
                        folder.file(ele.fileName, images[0]);
                    });
                }
            }, 100)
        })
        setTimeout(() => {
            // 生成二进制内容
            zip.generateAsync({ type: "blob" }).then(function(content) {
                // 创建URL
                const url = URL.createObjectURL(content);
                // 创建a标签并模拟点击下载
                const a = document.createElement("a");
                a.href = url;
                a.download =  companyName + moment().format('YYYY-MM-DD')
                document.body.appendChild(a)
                a.click();
                // 清除URL
                document.body.removeChild(a)
            });
        }, 3000)
    }
}
